﻿ using System;
 using System.Collections.Generic;
 using System.Text;
 using System.IO;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 
 namespace CustomsSystem.Common
 {
     /// <summary>
     /// 需要在调用的页面中必须重写VerifyRenderingInServerForm方法，否则将出现错误
     ///  public override void VerifyRenderingInServerForm( Control control )
     ///  {
     ///  }
     /// 
     /// </summary>
     public class HtmlDataToDoc
     {
         private Page _InvokePage;
         /// <summary>
         /// 调用的页面
         /// </summary>
         public Page InvokePage
         {
             get { return _InvokePage; }
             set { _InvokePage = value; }
         }              
         
         /// <summary>
         /// 构造函数
         /// </summary>
         /// <param name="invoke_page">调用的页面，参数传this.Page</param>
         public HtmlDataToDoc(Page invoke_page)
         {
             _InvokePage = invoke_page;
         }
 
         /// <summary>
         /// Table导出excel
         /// </summary>
         /// <param name="tab"></param>
         /// <param name="fileName"></param>
         public void TableDataToExcel(System.Web.UI.WebControls.Table tab, string fileName)
         {
 
             if (fileName.ToLower().IndexOf(".xls") == -1)
             {
                 fileName = fileName + ".xls";
             }
 
             TableExport(tab, fileName, "application/ms-excel"); 
         }
 
         /// <summary>
         /// Table导出Word
         /// </summary>
         /// <param name="tab"></param>
         /// <param name="fileName"></param>
         //public void TableDataToWord(System.Web.UI.HtmlControls.HtmlTable tab, string fileName)
         //{
 
         //    if (fileName.ToLower().IndexOf(".doc") == -1)
         //    {
         //        fileName = fileName + ".doc";
         //    }
 
         //    TableExport(tab, fileName, "application/ms-word"); 
         //}
 
         /// <summary>
         /// 导入数据，保存文档
      /// </summary>
         /// <param name="tab"></param>
         /// <param name="fileName"></param>
         /// <param name="typeName"></param>
         private void TableExport(System.Web.UI.WebControls.Table tab, string fileName, string typeName)
         {
             System.Web.HttpResponse httpResponse = _InvokePage.Response;
             httpResponse.Clear();
             httpResponse.Buffer = true;
             httpResponse.Charset = "GB2312";
             //Response.Charset = "UTF-8";
             httpResponse.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
             httpResponse.Write("<head>");
             httpResponse.Write("<meta http-equiv=Content-Type content=text/html;charset=GB2312>");
             httpResponse.Write("<!--[if gte mso 9]>");
             httpResponse.Write("<xml>");
             httpResponse.Write(" <x:ExcelWorkbook>");
             httpResponse.Write("  <x:ExcelWorksheets>");
             httpResponse.Write("   <x:ExcelWorksheet>");
             httpResponse.Write("    <x:Name>" + fileName + "</x:Name>");
             httpResponse.Write("    <x:WorksheetOptions>");
             httpResponse.Write("      <x:Print>");
             httpResponse.Write("       <x:ValidPrinterInfo />");
             httpResponse.Write("      </x:Print>");
             httpResponse.Write("    </x:WorksheetOptions>");
             httpResponse.Write("   </x:ExcelWorksheet>");
             httpResponse.Write("  </x:ExcelWorksheets>");
             httpResponse.Write("</x:ExcelWorkbook>");
             httpResponse.Write("</xml>");
             httpResponse.Write("<![endif]-->");
             httpResponse.Write("</head>");
             httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8).ToString());
             httpResponse.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
             httpResponse.ContentType = typeName;
             _InvokePage.EnableViewState = false;
             StringWriter tw = new StringWriter();
             System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
             tab.RenderControl(hw);
             httpResponse.Write(tw.ToString());
             httpResponse.End();
         }
     }
}
